package logic

import (
	"context"
	"srv/models/mysqlinit"
	"time"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListHouseLogic {
	return &ListHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListHouseLogic) ListHouse(in *srv.ListHouseRequest) (*srv.ListHouseResponse, error) {
	// todo: add your logic here and delete this line
	var house []mysqlinit.House
	mysqlinit.DB.Debug().Order("created_at desc").Where("user_id = ?", in.UserId).Find(&house)
	var list []*srv.ListHouse
	for _, m := range house {
		listHouse := srv.ListHouse{
			UserId:     in.UserId,
			City:       m.City,
			Address:    m.Address,
			HouseCode:  m.HouseCode,
			HouseFloor: m.HouseFloor,
			HouseAcre:  m.HouseAcre,
			HousePrice: float32(m.HousePrice),
			Describe:   m.Describe,
			CreatedAt:  m.CreatedAt.Format(time.DateTime),
		}
		list = append(list, &listHouse)
	}
	return &srv.ListHouseResponse{
		List: list,
	}, nil
}
